<template>
	<view>
		<!-- 编辑纸条 -->
		<u-toast ref="uToast"></u-toast>
		<u--input placeholder="请留下您的联系方式" border="surround" v-model="relative" clearable></u--input>
		<text-rich :initContent="initContent" ref="textRich"></text-rich>
		<button @tap="save">保存至草稿</button>

	    <button @click="restore">还原</button>
				<button @tap="submit">发布</button>
	</view>
</template>

<script>
	import textRich from '../../components/textRich/textRich.vue'

	export default {
		components: {
			textRich,

		},
		data() {
			return {
				relative: ''
			}
		},
		computed: {
			initContent() {
				this.relative=this.$store.state.editPage.relativeMethod
				return this.$store.state.editPage.htmlContent
			}
		},
		methods: {
			restore(){
				this.$store.commit('editPage/SET_CONTENT', {
					textContent:'',
					htmlContent:'',
					relativeMethod:''
				})
			},
			save() {
				this.$refs.textRich.editorCtx.getContents({ //获取htmlContent
					success: (res) => {
						console.log(res.html)
						this.$store.commit('editPage/SET_CONTENT', {
							textContent: res.text,
							htmlContent: res.html,
							relativeMethod:this.relative
						})
					}
				})
			},
			submit() {
				if(!this.relative){
					this.$refs.uToast.show({
						message: '请填写联系方式！',
						type: 'error',
						duration: 1000
					})
					return
				}
				this.$refs.textRich.editorCtx.getContents({ //获取htmlContent
					success: ({
						html,
						text
					}) => {
						text=text.replace(/\s*/g,'')
						if (!text) {
							this.$refs.uToast.show({
								message: '内容为空！发布失败',
								type: 'error',
								duration: 1000
							})
							return
						}
						this.$myRquest({
							url: '/pond/create',
							method: "post",
							data: {
								wechat_account: this.relative,
								description: html
							}
						}).then(res => {
							if(res.data.code==200){
								this.$refs.uToast.show({
									message: '发布成功!',
									type: 'success',
									duration: 1000
								})
							}else{
								this.$refs.uToast.show({
									message: '发布失败!',
									type: 'error',
									duration: 1000
								})
							}
						
						})
					}
				})
			}
		}
	}
</script>

<style>
</style>
